package edu.thu.studio.ormdesigner.wizard.page;

import java.io.IOException;
import java.io.InputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;

import edu.thu.studio.ormdesigner.model.ORMModel;
import edu.thu.studio.ormdesigner.persistent.Persistent;
import edu.thu.studio.ormdesigner.util.Properties;
import edu.thu.studio.ormdesigner.util.Utils;

public class WizardNewFilePage extends WizardNewFileCreationPage {
    
    private ORMModel diagram;
    
    private static final String EXTENSION = ".orm";
    
    public WizardNewFilePage(IStructuredSelection selection) {
        super(Properties.wizard_new_title, selection);
        this.setTitle(Properties.wizard_new_title);
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public void createControl(Composite parent) {
        super.createControl(parent);
        this.setFileName("newfile");
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean validatePage() {
        boolean valid = super.validatePage();
        if (valid) {
            String fileName = this.getFileName();
            if (fileName.indexOf(".") != -1 && !fileName.endsWith(EXTENSION)) {
                this.setErrorMessage(Properties.error_file_extension);
                valid = false;
            }
        }
        if (valid) {
            String fileName = this.getFileName();
            if (fileName.indexOf(".") == -1) {
                fileName = fileName + EXTENSION;
            }
            IWorkspace workspace = ResourcesPlugin.getWorkspace();
            IWorkspaceRoot root = workspace.getRoot();
            
            IPath containerPath = this.getContainerFullPath();
            IPath newFilePath = containerPath.append(fileName);
            
            if (root.getFile(newFilePath).exists()) {
                this.setErrorMessage("'" + fileName + "' " + Properties.error_file_exists);
                valid = false;
            }
        }
        
        if (valid) {
            this.setMessage(Properties.wizard_new_message);
        }
        
        return valid;
    }
    
    public void createORM() {
        this.diagram = new ORMModel();
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    protected InputStream getInitialContents() {
        Persistent persistent = Persistent.getInstance();
        
        try {
            InputStream in = persistent.createInputStream(this.diagram);
            return in;
            
        } catch (IOException e) {
            Utils.showExceptionDialog(e);
        }
        
        return null;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public IFile createNewFile() {
        String fileName = this.getFileName();
        if (fileName.indexOf(".") == -1) {
            this.setFileName(fileName + EXTENSION);
        }
        
        return super.createNewFile();
    }
    
}
